<?php

namespace App\Services\spider;

abstract class ITypeParser
{
    public string $type;
    public string $pattern;//url正则表达式

    /**
     * @param string $type
     * @param string $pattern
     */
    public function __construct(string $type, string $pattern)
    {
        $this->type = $type;
        $this->pattern = $pattern;
    }

    public function isTarget(string $url): bool
    {
        return !!preg_match($this->pattern, $url);
    }

    public function parse(IUrParser $parser, string $url, string $taskNo): ParseResult
    {
        $r = $parser->parse($url, $taskNo);
        if ($r->success) {
            return $this->parseHtml($url, $r->content);
        }
        return ParseResult::of();
    }

    public abstract function parseHtml(string $url, string $htmlContent): ParseResult;

}